package cn.zhz.controller;


import cn.zhz.pojo.Constants;
import cn.zhz.pojo.PageSupport;
import cn.zhz.pojo.Role;
import cn.zhz.pojo.User;
import cn.zhz.service.role.RoleService;
import cn.zhz.service.user.UserService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController{
    private static Logger logger = Logger.getLogger(UserController.class);

    @Resource
    UserService userService;

    @Resource
    RoleService roleService;

    @RequestMapping(value = "/userlist.html")
    public String getUserList(Model model,
                              @RequestParam(value = "queryname", required = false) String queryUserName,
                              @RequestParam(value = "queryUserRole", required = false) String queryUserRole,
                              @RequestParam(value = "pageIndex", required = false) String pageIndex) throws Exception {
        logger.info("getUserList ---- > queryUserName: " + queryUserName);
        logger.info("getUserList ---- > queryUserRole: " + queryUserRole);
        logger.info("getUserList ---- > pageIndex: " + pageIndex);
        int _queryUserRole = 0;
        List<User> userList = null;
        //设置页面容量
        int pageSize = Constants.pageSize;
        //当前页码
        int currentPageNo = 1;

        if (queryUserName == null) {
            queryUserName = "";
        }
        if (queryUserRole != null && !queryUserRole.equals("")) {
            _queryUserRole = Integer.parseInt(queryUserRole);
        }

        if (pageIndex != null) {
            try {
                currentPageNo = Integer.valueOf(pageIndex);
            } catch (NumberFormatException e) {
                return "redirect:/user/syserror.html";
                //response.sendRedirect("syserror.jsp");
            }
        }
        //总数量（表）
        int totalCount = userService.getUserCount(queryUserName, _queryUserRole);
        //总页数
        PageSupport pages = new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        //控制首页和尾页
        if (currentPageNo < 1) {
            currentPageNo = 1;
        } else if (currentPageNo > totalPageCount) {
            currentPageNo = totalPageCount;
        }
        userList = userService.getUserList(queryUserName, _queryUserRole, currentPageNo, pageSize);
        model.addAttribute("userList", userList);
        List<Role> roleList = null;
        roleList = roleService.getRoleList();
        model.addAttribute("roleList", roleList);
        model.addAttribute("queryUserName", queryUserName);
        model.addAttribute("queryUserRole", queryUserRole);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", currentPageNo);
        return "userlist";
    }

    @RequestMapping(value = "/useradd.html", method = RequestMethod.GET)
    public String addUser(@ModelAttribute("user") User user) {
        return "useradd";
    }


    /*@RequestMapping(value = "/addsave.html",method = RequestMethod.POST)
    public String addUserSave(User user,HttpSession session){
        user.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
        user.setCreationDate(new Date());
        if(userService.adduser(user)>0){
            return "redirect:/userlist.html";
        }
        return "useradd";
    }*/

    @RequestMapping(value = "/addsave.html", method = RequestMethod.POST)
    public String addUserSave(User user, HttpServletRequest request, HttpSession session, @RequestParam(value = "attachs", required = false) MultipartFile[] attachs) {
        String idPicPath = null;
        String workPicPath = null;
        String errorInfo = null;
        boolean flag = true;
        String path = request.getSession().getServletContext().getRealPath("statics" + File.separator + "uploadfiles");
        logger.info("uploadFile path=====>" + path);

        if (flag) {
            user.setCreatedBy(((User) session.getAttribute(Constants.USER_SESSION)).getId());
            user.setCreationDate(new Date());
            user.setIdPicPath(idPicPath);
            user.setWorkPicPath(workPicPath);
            if (userService.adduser(user) > 0) {
                return "redirect:/userlist.html";
            }
        }
        return "useradd";
    }

    @RequestMapping(value = "/add.html", method = RequestMethod.GET)
    public String add(@ModelAttribute("user") User user) {
        return "useradd";
    }

    @RequestMapping(value = "/add.html", method = RequestMethod.POST)
    public String addSave(/*@Valid*/ User user, /*BindingResult bindingResult,*/HttpSession session) {
        /*if(bindingResult.hasErrors()){
            logger.debug("ass user validated  has error======");
            return "useradd";
        }*/
        user.setCreatedBy(((User) session.getAttribute(Constants.USER_SESSION)).getId());
        user.setCreationDate(new Date());
        if (userService.adduser(user) > 0) {
            return "redirect:/userlist.html";
        }
        return "useradd";
    }

    @RequestMapping(value = "/usermodify.html", method = RequestMethod.GET)
    public String getUserById(@RequestParam String uid, Model model) {
        logger.debug("getUserById uid========" + uid);
        User user = userService.getUserById(uid);
        model.addAttribute("user", user);
        return "usermodify";
    }

    @RequestMapping(value = "/usermodifysave.html", method = RequestMethod.POST)
    public String modifyUserSave(User user, HttpSession session) {
        logger.debug("modifyUserSave userid========" + user.getId());
        user.setCreatedBy(((User) session.getAttribute(Constants.USER_SESSION)).getId());
        user.setModifyDate(new Date());
        if (userService.modify(user)) {
            return "redirect:/userlist.html";
        }
        return "usermodify";
    }

    @RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
    public String view(@PathVariable String id, Model model) {
        logger.debug("view uid========" + id);
        User user = userService.getUserById(id);
        model.addAttribute("user", user);
        return "userview";
    }

    @RequestMapping(value = "/ucexist.html")
    @ResponseBody
    public Object userCodeIsExit(@RequestParam String userCode) {
        logger.debug("userCodeIsExit userCode :" + userCode);
        HashMap<String, String> resultMap = new HashMap<String, String>();
        if (StringUtils.isNullOrEmpty(userCode)) {
            resultMap.put("userCode", "exist");
        } else {
            User user = userService.selectUserCodeExist(userCode);
            if (null != user)
                resultMap.put("userCode", "exist");
            else
                resultMap.put("userCode", "noexist");
        }
        return JSONArray.toJSONString(resultMap);
    }

    @RequestMapping(value = "/view.html",
            method = RequestMethod.GET)
    @ResponseBody
    public User view(@RequestParam String id) {
        logger.debug("view id :" + id);
        User user =new User();
        try {
            user = userService.getUserById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

}
